package org.example.goods.service.impl;

import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.Goods;
import org.example.goods.mapper.GoodsMapper;
import org.example.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;

@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private DataSource dataSource;

    @Override
    public Goods findGoodsById(Long id) {
        return goodsMapper.selectById(id);
    }

    @GlobalTransactional
    @Override
    public void reduceGoodsStock(Long id, Integer amount) {
        System.out.println(dataSource.getClass().getName());
        log.info("in global transaction: {}", RootContext.inGlobalTransaction());
        log.info("xid: {}", RootContext.getXID());
        goodsMapper.reduceGoodsStock(id, amount);
        int i = 1 / 0;
    }
}
